package bscorp.appbase.bmo;


import android.util.Log;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bscorp.appbase.AppManager;
import bscorp.appbase.db.M4Dao;
import bscorp.appbase.model.User;

/**
 * description: 用户数据处理
 * autour: jamie
 * date:   2017/9/13.
 * update:
 * version:
 */
public class UserBmo {
    private static String TAG = "UserBmo";

    /**
     * 保存用户信息
     *
     * @param model
     */
    public boolean saveUserInfo(User model) {
        try {
            M4Dao.getInstance(AppManager.context).add(model, User.class);
            return true;
        } catch (SQLException e) {
            Log.e("jamie", "---saveUserInfo  error---");
            return false;
        }
    }

    private List<User> getUserByValues(Map<String, String> map) {
        List<User> list = null;
        try {
            list = M4Dao.getInstance(AppManager.context).getByValues(map, User.class);
        } catch (SQLException e) {
            Log.e("jamie", "---getUserInfo error---" + e.getMessage());
        }
        return list;
    }

    /**
     * 读取用户信息
     *
     * @return
     */
    public User getUserById(int id) {
        try {
            User user = (User) M4Dao.getInstance(AppManager.context).get(id, User.class);
            return user;
        } catch (SQLException e) {
            Log.e("jamie", "---getUserInfo error---" + e.getMessage());
        }
        return null;
    }

    /**
     * 读取用户信息
     *
     * @return
     */
    public User getUserByPhone(String phone) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phone", phone);
        List<User> list = getUserByValues(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 读取用户信息
     *
     * @return
     */
    public User getUserByPhoneAndPass(String phone, String password) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phone", phone);
        map.put("password", password);
        List<User> list = getUserByValues(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 读取用户信息
     *
     * @return
     */
    public User getUserByLoginAndPass(String login, String password) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("login", login);
        map.put("password", password);
        List<User> list = getUserByValues(map);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

}
